package transactionExchange.pkg;

import com.google.appengine.api.datastore.*;
import com.google.appengine.repackaged.com.google.common.collect.Iterables;
import java.util.Date;

public class Store {

	Entity book;
    Key bookKey;
    static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    public Store(String bookName, String bookAuthor, String nickName){

         bookKey = KeyFactory.createKey("BookList", bookName);
         book = new Entity(bookKey);
         book.setProperty("Key",bookKey);
         book.setProperty("Book Name",bookName);
         book.setProperty("Book Author",bookAuthor);
         book.setProperty("Date",new Date());
         book.setProperty("User",nickName);

         datastore.put(book);
    }

    public static void delBook(Key bookKey){
        datastore.delete(bookKey);
    }

/*
   public Key getKey(){
        return book.getKey();
           }

    public  String getAuthor(Key bookKey){
        try {
        book = datastore.get(bookKey);
        return (String) book.getProperty("Book Author");
        } catch (EntityNotFoundException e) {
            return "";//NO book?
            }
    }

    public String getName(Key bookKey){
        try {
        book = datastore.get(bookKey);
        return (String) book.getProperty("Book Name");
        } catch (EntityNotFoundException e) {
            return "";//NO book?
            }
    }

    public String getTime(Key bookKey){
        try {
        book = datastore.get(bookKey);
        return (String) book.getProperty("Date");
        } catch (EntityNotFoundException e) {
            return "";//NO book?
            }
    }

    public String getUser(Key bookKey){
        try {
        book = datastore.get(bookKey);
        return (String) book.getProperty("User");
        } catch (EntityNotFoundException e) {
            return "";//NO book?
            }

    }
*/
    public static Iterable<Entity> queryData(String sortBy) {
        Query query = new Query("BookList").addSort(sortBy, Query.SortDirection.ASCENDING);
        return datastore.prepare(query).asIterable();
    }
}
